using System;
using System.Runtime.CompilerServices;
using HslCommunication;

namespace Catastrophe.AutoSulution.TaskM
{
	public class TaskFormater : ITaskFormater
	{
		/// <summary>
		/// 步骤详情
		/// </summary>
		/// <param name="ischedule">调度对象</param>
		/// <param name="taskParam">调度参数</param>
		/// <returns></returns>
		public virtual string GetTaskStepDetails(IScheduleControl ischedule, TaskComplete taskParam)
		{
			TaskDescribeEntity taskDescribeEntity = BaseLogic.TaskCompleteEntityFormat(taskParam);
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(1, 2);
			defaultInterpolatedStringHandler.AppendFormatted(taskDescribeEntity.Index);
			defaultInterpolatedStringHandler.AppendLiteral(".");
			defaultInterpolatedStringHandler.AppendFormatted(taskDescribeEntity.Describe);
			return defaultInterpolatedStringHandler.ToStringAndClear();
		}

		/// <summary>
		/// 任务状态
		/// </summary>
		/// <param name="ischedule">调度对象</param>
		/// <param name="taskParam">调度参数</param>
		/// <returns></returns>
		public virtual string GetTaskRunState(IScheduleControl ischedule, TaskComplete taskParam)
		{
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(1, 2);
			defaultInterpolatedStringHandler.AppendFormatted(taskParam.RunStatus);
			defaultInterpolatedStringHandler.AppendLiteral("-");
			defaultInterpolatedStringHandler.AppendFormatted(DateTime.Now, "ss:fff");
			return defaultInterpolatedStringHandler.ToStringAndClear();
		}

		/// <summary>
		/// 获取运行结果
		/// </summary>
		/// <param name="ischedule">调度对象</param>
		/// <param name="taskParam">调度参数</param>
		/// <returns></returns>
		/// <exception cref="T:System.NotImplementedException"></exception>
		public virtual OperateResult RunResult(IScheduleControl ischedule, TaskComplete taskParam)
		{
			OperateResult operateResult = new OperateResult();
			if (taskParam?.Exception == null)
			{
				operateResult.IsSuccess = true;
				return operateResult;
			}
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(0, 2);
			defaultInterpolatedStringHandler.AppendFormatted(taskParam.Exception);
			defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
			operateResult.Message = defaultInterpolatedStringHandler.ToStringAndClear();
			return operateResult;
		}

		/// <summary>
		/// 当前任务预览-TaskComplete.Param.ToString()
		/// 需要对象TaskComplete.Param重写ToString()方法
		/// </summary>
		/// <param name="ischedule">调度对象</param>
		/// <param name="taskParam">调度参数</param>
		/// <returns></returns>
		public virtual string GetTaskNowDescribe(IScheduleControl ischedule, TaskComplete taskParam)
		{
			string describe = taskParam.ScheduleAb.Describe;
			string text = taskParam?.Param?.ToString();
			if (text != null && text != taskParam.Param.GetType().FullName)
			{
				return taskParam?.Param?.ToString() ?? "";
			}
			return "等待新任务";
		}

		/// <summary>
		/// 获取任务进度
		/// </summary>
		/// <param name="ischedule">调度对象</param>
		/// <param name="taskParam">调度参数</param>
		/// <returns></returns>
		public virtual Tuple<int, int> GetProcess(IScheduleControl ischedule, TaskComplete taskParam)
		{
			int item = ischedule.StepIndexNow(taskParam.MethodName);
			return new Tuple<int, int>(item, ischedule.MaxStep);
		}
	}
}
